//
//  SLCancellationAccountNextVC.m
//  BXlive
//
//  Created by sweetloser on 2020/8/28.
//  Copyright © 2020 cat. All rights reserved.
//

#import "SLCancellationAccountNextVC.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <UMPush/UMessage.h>
#import <Masonry/Masonry.h>

@interface SLCancellationAccountNextVC ()

@property(nonatomic,strong)UIView *navigationView;

@property(nonatomic,strong)UITextField *yzmTextField;

@property(nonatomic,strong)UIButton *getCodeBtn;

@property(nonatomic,strong)UIButton *determineBtn;

@property(nonatomic,strong)UILabel *noticeLabel;

@property(nonatomic,assign)int limit;

@property(nonatomic,assign)NSTimer *timer;

@end

@implementation SLCancellationAccountNextVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    [self setupNavigationView];
    [self setupViews];
    // Do any additional setup after loading the view.
}

-(void)setupViews{
    WS(weakSelf);
    _noticeLabel = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:[NSString stringWithFormat:NSLocalizedString(@"请获取验证码，并于有效时间内完成验证。", nil)] Font:SLPFFont(16) TextColor: sl_textColors];
    [self.view addSubview:_noticeLabel];
    [_noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(30));
    }];
    _noticeLabel.numberOfLines = 0;
    
    
    
    UIView *codeView = [UIView new];
    [self.view addSubview:codeView];
    
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(-__ScaleWidth(12));
        make.height.mas_equalTo(__ScaleWidth(48));
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(104));
    }];
    codeView.backgroundColor = sl_subBGColors;
    codeView.layer.masksToBounds = YES;
    codeView.layer.cornerRadius = __ScaleWidth(24);
    
    _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [codeView addSubview:_getCodeBtn];
    [_getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(__ScaleWidth(100));
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(__ScaleWidth(48));
    }];
    [_getCodeBtn setTitle:NSLocalizedString(@"获取验证码", nil) forState:BtnNormal];
    _getCodeBtn.titleLabel.font = SLPFFont(14);
    [_getCodeBtn sl_setTitleColor:sl_textColors];
    [_getCodeBtn addTarget:self action:@selector(getCodeBtnAction) forControlEvents:BtnTouchUpInside];
    
    _yzmTextField = [[UITextField alloc] init];
    [codeView addSubview:_yzmTextField];
    [_yzmTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(20));
        make.height.mas_equalTo(__ScaleWidth(20));
        make.centerY.mas_equalTo(0);
        make.right.equalTo(weakSelf.getCodeBtn.mas_left).offset(-__ScaleWidth(10));
    }];
    
    _yzmTextField.font = SLPFFont(14);
    _yzmTextField.textColor = sl_textColors;
    _yzmTextField.placeholder = NSLocalizedString(@"请输入验证码", nil);
    
    //    立即注销
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(-__ScaleWidth(12));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.top.equalTo(codeView.mas_bottom).offset(__ScaleWidth(20));
    }];
    [nextBtn setTitle:NSLocalizedString(@"立即注销", nil) forState:BtnNormal];
    [nextBtn sl_setTitleColor:sl_textColors];
    [nextBtn.titleLabel setFont: SLBFont(16)];
    nextBtn.layer.cornerRadius = __ScaleWidth(22);
    nextBtn.layer.masksToBounds = YES;
    nextBtn.backgroundColor = sl_subBGColors;
    
    [nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:BtnTouchUpInside];
    _determineBtn = nextBtn;
}

-(void)getCodeBtnAction{
    
    NSString *phone_code = [BXAppInfo appInfo].code;
    
    NSString *phone = [BXLiveUser currentBXLiveUser].phone;
    
    NSString *scene = @"user_logoff";
    WS(weakSelf);
    [NewHttpManager sendSmsCodeWithPhone:phone scene:scene phoneCode:phone_code success:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        if (flag) {
            NSDictionary *data = jsonDic[@"data"];
            weakSelf.limit = [data[@"limit"] intValue];
            [weakSelf updateUIAfterGetCode];
            NSLog(@"%@",jsonDic);
        }
        [SLProgressHUD slShowInfoWithMessage:jsonDic[@"msg"]];
    } failure:^(NSError *error) {
        
    }];
}

-(void)updateUIAfterGetCode{
    _noticeLabel.text = [NSString stringWithFormat:NSLocalizedString(@"验证码已发送至您的手机%@，请于有效时间内完成验证。", nil),[BXLiveUser currentBXLiveUser].phone];
    _getCodeBtn.userInteractionEnabled = NO;
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeSecs) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

-(void)changeSecs{
    [_getCodeBtn setTitle:[NSString stringWithFormat:NSLocalizedString(@"%02d s", nil),_limit--] forState:BtnNormal];
    if (_limit < 0) {
        [_timer invalidate];
        _timer = nil;
        _limit = 0;
        _getCodeBtn.userInteractionEnabled = YES;
        [_getCodeBtn setTitle:NSLocalizedString(@"获取验证码", nil) forState:BtnNormal];
        _noticeLabel.text = NSLocalizedString(@"验证码已过期，请重新获取验证码，并于有效时间内完成验证。", nil);
    }
}

-(void)nextBtnAction{
    if (_yzmTextField.text.length < 6 || _yzmTextField.text.length > 6) {
        [SLProgressHUD slShowInfoWithMessage:NSLocalizedString(@"请输入6位有效验证码", nil)];
        return;
    }
    
    NSString *yzm = _yzmTextField.text;
    NSString *phone_code = [BXAppInfo appInfo].code;
    NSString *phone = [BXLiveUser currentBXLiveUser].phone;
    
    [[NewHttpManager sharedNetManager] POST:@"s=User.logOffUser" parameters:@{@"phone_code":phone_code,@"code":yzm,@"phone":phone} success:^(id  _Nonnull responseObject) {
        
        NSLog(@"%@",responseObject);
        NSNumber *code = responseObject[@"code"];
        if ([code intValue] == 0) {
            NSString *userId = [BXLiveUser currentBXLiveUser].user_id;
            [UMessage removeAlias:userId type:@"" response:^(id  _Nullable responseObject, NSError * _Nullable error) {
                
            }];

            BXLiveUser *liveUser = [[BXLiveUser alloc]init];
            liveUser.user_id = @"-999";
            [BXLiveUser setCurrentBXLiveUser:liveUser];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kDidLogoutNotification object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
            [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
    
}

-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"注销账号", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
